home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / bash_completion.d / ri < prev    next >
Encoding:
Text File  |  2010-11-16  |  2.9 KB  |  97 lines

  1. # ri completion for Ruby documentation by Ian Macdonald <ian@caliban.org>
  2.  
  3. have ri && {
  4. ri_get_methods()
  5. {
  6.     local regex
  7.  
  8.     if [ "$ri_version" = integrated ]; then
  9.         if [ -z "$separator" ]; then
  10.             regex="(Instance|Class)"
  11.         elif [ "$separator" = "#" ]; then
  12.             regex=Instance
  13.         else
  14.             regex=Class
  15.         fi
  16.  
  17.         COMPREPLY=( ${COMPREPLY[@]} \
  18.             "$( ri ${classes[@]} 2>/dev/null | ruby -ane \
  19.             'if /^'"$regex"' methods:/.../^------------------|^$/ and \
  20.             /^ / then print $_.split(/, |,$/).grep(/^[^\[]*$/).join("\n"); \
  21.             end' | sort -u )" )
  22.     else
  23.         # older versions of ri didn't distinguish between class/module and
  24.         # instance methods
  25.         COMPREPLY=( ${COMPREPLY[@]} \
  26.             "$( ruby -W0 $ri_path ${classes[@]} | ruby -ane \
  27.             'if /^-/.../^-/ and ! /^-/ and ! /^ +(class|module): / then \
  28.             print $_.split(/, |,$| +/).grep(/^[^\[]*$/).join("\n"); \
  29.             end' | sort -u )" )
  30.     fi
  31.     COMPREPLY=( $( compgen $prefix -W '${COMPREPLY[@]}' -- $method ) )
  32. }
  33.  
  34. # needs at least Ruby 1.8.0 in order to use -W0
  35. _ri()
  36. {
  37.     local cur class method prefix ri_path ri_version separator IFS
  38.     local -a classes
  39.  
  40.     COMPREPLY=()
  41.     _get_comp_words_by_ref cur
  42.  
  43.     ri_path=$(type -p ri)
  44.     # which version of ri are we using?
  45.     # -W0 is required here to stop warnings from older versions of ri
  46.     # from being captured when used with Ruby 1.8.1 and later
  47.     ri_version="$(ruby -W0 $ri_path -v 2>&1)" || ri_version=integrated
  48.     [ "$ri_version" != "${ri_version%200*}" ] && ri_version=integrated
  49.  
  50.     # need to also split on commas
  51.     IFS=$', \n\t'
  52.     if [[ "$cur" == [A-Z]*[#.]* ]]; then
  53.         [[ "$cur" == *#* ]] && separator=# || separator=.
  54.         # we're completing on class and method
  55.         class=${cur%$separator*}
  56.         method=${cur#*$separator}
  57.         classes=( $class )
  58.         prefix="-P $class$separator"
  59.         ri_get_methods
  60.         return 0
  61.     fi
  62.  
  63.     if [ "$ri_version" = integrated ]; then
  64.         # integrated ri from Ruby 1.9
  65.         classes=( $( ri -c | ruby -ne 'if /^\s*$/..$stdin.eof then \
  66.         if /, [A-Z]+/ then print; end; end' ) )
  67.     elif [ "$ri_version" = "ri 1.8a" ]; then
  68.         classes=( $( ruby -W0 $ri_path | \
  69.             ruby -ne 'if /^'"'"'ri'"'"' has/..$stdin.eof then \
  70.             if /^ .*[A-Z]/ then print; end; end' ))
  71.     else
  72.         classes=( $( ruby -W0 $ri_path | \
  73.             ruby -ne 'if /^I have/..$stdin.eof then \
  74.                 if /^ .*[A-Z]/ then print; end; end' ))
  75.     fi
  76.  
  77.     COMPREPLY=( $( compgen -W '${classes[@]}' -- "$cur" ) )
  78.     if [[ "$cur" == [A-Z]* ]]; then
  79.         # we're completing on class or module alone
  80.         return 0
  81.     fi
  82.  
  83.     # we're completing on methods
  84.     method=$cur
  85.     ri_get_methods
  86. }
  87. complete -F _ri ri
  88. }
  89.  
  90. # Local variables:
  91. # mode: shell-script
  92. # sh-basic-offset: 4
  93. # sh-indent-comment: t
  94. # indent-tabs-mode: nil
  95. # End:
  96. # ex: ts=4 sw=4 et filetype=sh
  97.